﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using FileSystem.Services.IServices;
using FileSystem.Views;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Xamarin.Forms;

namespace FileSystem.ViewModels {
    /// <summary>
    /// 初始化页ViewModel
    /// </summary>
    public class InitializationPageViewModel : ViewModelBase {
        // ******** 构造函数

        /// <summary>
        /// 根目录项存储
        /// </summary>
        private IRootDirItemStorage _rootDirItemStorage;

        /// <summary>
        /// 目录项存储
        /// </summary>
        private IDirItemStorage _dirItemStorage;

        /// <summary>
        /// 用户存储
        /// </summary>
        private IUserStorage _userStorage;

        /// <summary>
        /// 物理磁盘块存储
        /// </summary>
        private IPhysicalDiskBlockStorage _physicalDiskBlockStorage;

        /// <summary>
        /// 磁盘块信息存储
        /// </summary>
        private IDiskBlockStorage _diskBlockStorage;

        /// <summary>
        /// 访问控制信息存储
        /// </summary>
        private IAccessControlInfoStorage _accessControlInfoStorage;

        /// <summary>
        /// 根导航服务
        /// </summary>
        private IRootNavigationService _rootNavigationService;

        /// <summary>
        /// 内容页激活服务
        /// </summary>
        private IContentPageActivationService _contentPageActivationService;

        /// <summary>
        /// 初始化页ViewModel
        /// </summary>
        /// <param name="rootDirItemStorage">根目录项存储</param>
        /// <param name="dirItemStorage">目录项存储</param>
        /// <param name="rootNavigationService">根导航服务</param>
        public InitializationPageViewModel(
            IRootDirItemStorage rootDirItemStorage,
            IDirItemStorage dirItemStorage, IUserStorage userStorage,
            IPhysicalDiskBlockStorage physicalDiskBlockStorage,
            IDiskBlockStorage diskBlockStorage,
            IAccessControlInfoStorage accessControlInfoStorage,
            IRootNavigationService rootNavigationService,
            IContentPageActivationService contentPageActivationService) {
            _rootDirItemStorage = rootDirItemStorage;
            _dirItemStorage = dirItemStorage;
            _userStorage = userStorage;
            _physicalDiskBlockStorage = physicalDiskBlockStorage;
            _diskBlockStorage = diskBlockStorage;
            _accessControlInfoStorage = accessControlInfoStorage;
            _rootNavigationService = rootNavigationService;
            _contentPageActivationService = contentPageActivationService;
        }

        // ******** 绑定命令

        /// <summary>
        /// 页面显示命令
        /// </summary>
        private RelayCommand _pageAppearingCommand;

        /// <summary>
        /// 页面显示命令
        /// </summary>
        public RelayCommand PageAppearingCommand =>
            _pageAppearingCommand ?? (_pageAppearingCommand =
                new RelayCommand(async () =>
                    await PageAppearingCommandFunction()));

        public async Task PageAppearingCommandFunction() {
            NavigationPage.SetHasNavigationBar(InitializationPage, false);

            if (!_rootDirItemStorage.IsInitialized())
                await _rootDirItemStorage.InitializeAsync();

            if (!_dirItemStorage.IsInitialized())
                await _dirItemStorage.InitializeAsync();

            if (!_userStorage.IsInitialized())
                await _userStorage.InitializeAsync();

            if (!_physicalDiskBlockStorage.IsInitialized())
                await _physicalDiskBlockStorage.InitializeAsync();

            if (!_diskBlockStorage.IsInitialized())
                await _diskBlockStorage.InitializeAsync();

            if (!_accessControlInfoStorage.IsInitialized())
                await _accessControlInfoStorage.InitializeAsync();

            await Task.Delay(1000);

            await _rootNavigationService.NavigateToAsync(RootNavigationConstants
                .MainPage);
        }

        /// <summary>
        /// NavigationPage
        /// </summary>
        public NavigationPage NavigationPage =>
            _navigationPage ?? (_navigationPage =
                Application.Current.MainPage as NavigationPage);

        /// <summary>
        /// NavigationPage
        /// </summary>
        private NavigationPage _navigationPage;

        /// <summary>
        /// InitializationPage
        /// </summary>
        public InitializationPage InitializationPage =>
            _initializationPage ?? (_initializationPage =
                NavigationPage.RootPage as InitializationPage);

        /// <summary>
        /// InitializationPage
        /// </summary>
        private InitializationPage _initializationPage;
    }
}